namespace ASPNET.StarterKit.IssueTracker.BusinessLogicLayer
{
    using ASPNET.StarterKit.IssueTracker.DataAccessLayer;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Web.UI.WebControls;

    public class CustomField
    {
        private ValidationDataType _DataType;
        private int _Id;
        private string _Name;
        private int _ProjectId;
        private bool _Required;
        private string _Value;

        public CustomField(int id, string value)
            : this(id, DefaultValues.GetProjectIdMinValue(), string.Empty, ValidationDataType.String, false, value)
        {
        }

        public CustomField(int projectId, string name, ValidationDataType dataType, bool required)
            : this(DefaultValues.GetCustomFieldIdMinValue(), projectId, name, dataType, required, string.Empty)
        {
        }

        public CustomField(int id, int projectId, string name, ValidationDataType dataType, bool required, string value)
        {
            this._Id = id;
            this._ProjectId = projectId;
            this._Name = name;
            this._DataType = dataType;
            this._Required = required;
            this._Value = value;
        }

        public static bool DeleteCustomField(int customFieldId)
        {
            if (customFieldId <= DefaultValues.GetPriorityIdMinValue())
            {
                throw new ArgumentOutOfRangeException("customFieldId");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().DeleteCustomField(customFieldId);
        }

        public static CustomFieldCollection GetCustomFieldsByIssueId(int issueId)
        {
            if (issueId <= DefaultValues.GetProjectIdMinValue())
            {
                throw new ArgumentOutOfRangeException("issueId");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetCustomFieldsByIssueId(issueId);
        }

        public static CustomFieldCollection GetCustomFieldsByIssueId_2(int issueId)
        {
            if (issueId <= DefaultValues.GetProjectIdMinValue())
            {
                throw new ArgumentOutOfRangeException("issueId");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetCustomFieldsByIssueId_2(issueId);
        }

        public static CustomFieldCollection GetCustomFieldsByProjectId(int projectId)
        {
            if (projectId <= DefaultValues.GetProjectIdMinValue())
            {
                throw new ArgumentOutOfRangeException("priorityId");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetCustomFieldsByProjectId(projectId);
        }

        public bool Save()
        {
            DataAccessLayerBaseClass DBLayer = DataAccessLayerBaseClassHelper.GetDataAccessLayer();
            if (this.Id <= DefaultValues.GetProjectIdMinValue())
            {
                int TempId = DBLayer.CreateNewCustomField(this);
                if (TempId > 0)
                {
                    this._Id = TempId;
                    return true;
                }
                return false;
            }
            return DBLayer.UpdateCustomField(this);
        }

        public static bool SaveCustomFieldValues(int issueId, int historyid, CustomFieldCollection fields)
        {
            if (fields == null)
            {
                throw new ArgumentOutOfRangeException("fields");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().SaveCustomFieldValues(issueId, historyid, fields);
        }

        public static bool SaveSingleCustomFieldValues(int issueId, string field)
        {
            if (StringType.StrCmp(field, "", false) == 0)
            {
                throw new ArgumentOutOfRangeException("field");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().SaveSingleCustomFieldValues(issueId, field);
        }

        public ValidationDataType DataType
        {
            get
            {
                return this._DataType;
            }
        }

        public int Id
        {
            get
            {
                return this._Id;
            }
        }

        public string Name
        {
            get
            {
                if ((this._Name == null) | (this._Name.Length == 0))
                {
                    return string.Empty;
                }
                return this._Name;
            }
            set
            {
                this._Name = value;
            }
        }

        public int ProjectId
        {
            get
            {
                return this._ProjectId;
            }
        }

        public bool Required
        {
            get
            {
                return this._Required;
            }
        }

        public string Value
        {
            get
            {
                if ((this._Value == null) | (this._Value.Length == 0))
                {
                    return string.Empty;
                }
                return this._Value;
            }
            set
            {
                this._Value = value;
            }
        }
    }
}

